/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package comunicatorserver.mobilecall;




import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.persistence.Query;



import javax.persistence.Persistence;
import javax.persistence.EntityManagerFactory;


/**
 *
 * @author rjsantos
 */
public class SendShortCall extends Thread {

    String sendtoquecmd="";

    public SendShortCall(String cmd){
        this.sendtoquecmd=cmd;
    }


    @Override
    public void run(){


        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ComunicatorServerPU");
        javax.persistence.EntityManager entityManager1 = java.beans.Beans.isDesignTime() ? null : emf.createEntityManager();



        Query q= entityManager1.createNativeQuery("SELECT id, nome, mail, telemovel FROM mails");
        //Query q= entityManager1.createNativeQuery("SELECT id, nome, mail, telemovel FROM mails where id=1");



        List l=q.getResultList();

        Iterator it = l.iterator();

        //enviar toques
        it = l.iterator();
        Vector ob;
        while(it.hasNext()){

            ob=(Vector) it.next();
            if(sendtoquecmd.length()>10){
                if(ob.get(3)!=null){
                    if(ob.get(3).toString().length()==9){
                        Process p;
                        try {
                            System.out.println("\n\n"+sendtoquecmd.replace("<num>",ob.get(3).toString())  );
                            p = Runtime.getRuntime().exec( sendtoquecmd.replace("<num>",ob.get(3).toString()) );
                            p.waitFor();
                        } catch (IOException ex) {
                            System.out.println("\n\nSendMail.java:70:IOException: "+ex.getMessage());
                        }
                        catch (InterruptedException ex) {
                            System.out.println("\n\nSendMail.java:73:InterruptedException: "+ex.getMessage());
                        }
                        p=null;
                    }
                }
            }
        }




        entityManager1.close();
        emf.close();



    }



}
